package cn.iocoder.yudao.module.fengdong.smartelderly.controller.app.activity;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.activity.vo.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.convert.activity.ActivityConvert;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.dataobject.activity.ActivityDO;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.mysql.activity.ActivityMapper;
import cn.iocoder.yudao.module.fengdong.smartelderly.service.activity.ActivityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 APP - 活动管理")
@RestController
@RequestMapping("/fengdong.smartelderly/activity")
@Validated
public class AppActivityController {

    @Resource
    private ActivityService activityService;
    @Resource
    private ActivityMapper activityMapper;

    @PostMapping("/create")
    @Operation(summary = "创建活动管理")
    public CommonResult<Long> createActivity(@Valid @RequestBody ActivityCreateReqVO createReqVO) {
        return success(activityService.createActivity(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新活动管理")
    public CommonResult<Boolean> updateActivity(@Valid @RequestBody ActivityUpdateReqVO updateReqVO) {
        activityService.updateActivity(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除活动管理")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteActivity(@RequestParam("id") Long id) {
        activityService.deleteActivity(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得活动管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ActivityRespVO> getActivity(@RequestParam("id") Long id) {
        ActivityDO activity = activityService.getActivity(id);
        //预览+1
        if(activity.getPreviewNum()==null) {
            activity.setPreviewNum(1L);
        }else {
            activity.setPreviewNum(activity.getPreviewNum()+1);
        }
        activityMapper.updateById(activity);
        return success(ActivityConvert.INSTANCE.convert(activity));
    }

    @GetMapping("/list")
    @Operation(summary = "获得活动管理列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    public CommonResult<List<ActivityRespVO>> getActivityList(@RequestParam("ids") Collection<Long> ids) {
        List<ActivityDO> list = activityService.getActivityList(ids);
        return success(ActivityConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得活动管理分页")
    public CommonResult<PageResult<ActivityRespVO>> getActivityPage(@Valid ActivityPageReqVO pageVO) {
        PageResult<ActivityDO> pageResult = activityService.getActivityPage(pageVO);
        return success(ActivityConvert.INSTANCE.convertPage(pageResult));
    }

}
